Display images from telescopes¶
In [1]:
Copied!
import ctapipe
print(ctapipe.__version__)
import ctapipe
print(ctapipe.__version__)
0.17.0
In [2]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from ctapipe.instrument import SubarrayDescription
from ctapipe.visualization import CameraDisplay
from ctapipe.io import TableLoader
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
from ctapipe.instrument import SubarrayDescription
from ctapipe.visualization import CameraDisplay
from ctapipe.io import TableLoader
from pathlib import Path
/home/runner/micromamba/envs/ctaodl2/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
In [3]:
Copied!
dl2_proton_filename = "../data/proton_with_images_00.dl2.h5"
dl2_gamma_filename = "../data/gamma-diffuse_with_images_00.dl2.h5"
dl2_proton_filename = "../data/proton_with_images_00.dl2.h5"
dl2_gamma_filename = "../data/gamma-diffuse_with_images_00.dl2.h5"
In [4]:
Copied!
subarray = SubarrayDescription.from_hdf(dl2_proton_filename)
subarray = SubarrayDescription.from_hdf(dl2_proton_filename)
In [5]:
Copied!
def read_table(filename, tel_ids=None, stop=None):
"""
Read data from a file and return it as a pandas DataFrame.
Parameters
----------
filename : str
The path to the input file.
tel_ids : list or None, optional
A list of telescope IDs to include in the data. If None, all telescopes are included. Default is None.
stop : int or None, optional
The number of events to read from the file. If None, all events are read. Default is None.
Returns
-------
pandas.DataFrame
The data read from the file as a pandas DataFrame.
"""
loader = TableLoader(
input_url=filename,
load_dl1_parameters=True,
load_dl2=False,
load_instrument=False,
load_simulated=False,
load_true_parameters=False,
load_dl1_images=True,
)
data = loader.read_telescope_events(telescopes=tel_ids, stop=stop)
return data
def read_table(filename, tel_ids=None, stop=None):
"""
Read data from a file and return it as a pandas DataFrame.
Parameters
----------
filename : str
The path to the input file.
tel_ids : list or None, optional
A list of telescope IDs to include in the data. If None, all telescopes are included. Default is None.
stop : int or None, optional
The number of events to read from the file. If None, all events are read. Default is None.
Returns
-------
pandas.DataFrame
The data read from the file as a pandas DataFrame.
"""
loader = TableLoader(
input_url=filename,
load_dl1_parameters=True,
load_dl2=False,
load_instrument=False,
load_simulated=False,
load_true_parameters=False,
load_dl1_images=True,
)
data = loader.read_telescope_events(telescopes=tel_ids, stop=stop)
return data
In [6]:
Copied!
def frame_strip(display):
"""
Remove all frame elements from a CameraDisplay object.
Parameters
----------
display : ctapipe.visualization.CameraDisplay
The CameraDisplay object to modify.
"""
display.axes.set_frame_on(False) # Remove the frame
display.axes.set_title('') # Remove the title
display.axes.set_xticks([]) # Remove x-axis ticks
display.axes.set_yticks([]) # Remove y-axis ticks
display.axes.set_xlabel('') # Remove x-axis label
display.axes.set_ylabel('') # Remove y-axis label
display.add_frame_name((1, 1, 1, 0)) # Remove "CameraFrame" by setting alpha to 0
display.cmap.set_bad(alpha=0) # Make the background transparent
for text in display.axes.texts:
if "Frame" in text.get_text(): # Match the label text
text.set_visible(False) # Hide it
def frame_strip(display):
"""
Remove all frame elements from a CameraDisplay object.
Parameters
----------
display : ctapipe.visualization.CameraDisplay
The CameraDisplay object to modify.
"""
display.axes.set_frame_on(False) # Remove the frame
display.axes.set_title('') # Remove the title
display.axes.set_xticks([]) # Remove x-axis ticks
display.axes.set_yticks([]) # Remove y-axis ticks
display.axes.set_xlabel('') # Remove x-axis label
display.axes.set_ylabel('') # Remove y-axis label
display.add_frame_name((1, 1, 1, 0)) # Remove "CameraFrame" by setting alpha to 0
display.cmap.set_bad(alpha=0) # Make the background transparent
for text in display.axes.texts:
if "Frame" in text.get_text(): # Match the label text
text.set_visible(False) # Hide it
In [7]:
Copied!
protons = read_table(dl2_proton_filename, stop=1000)
gammas = read_table(dl2_gamma_filename, stop=1000)
protons = read_table(dl2_proton_filename, stop=1000)
gammas = read_table(dl2_gamma_filename, stop=1000)
In [8]:
Copied!
# select events above a certain intensity threshold
min_intensity = 100
protons = protons[protons['hillas_intensity'] > min_intensity]
gammas = gammas[gammas['hillas_intensity'] > min_intensity]
print(f"Number of protons: {len(protons)}")
print(f"Number of gammas: {len(gammas)}")
# select events above a certain intensity threshold
min_intensity = 100
protons = protons[protons['hillas_intensity'] > min_intensity]
gammas = gammas[gammas['hillas_intensity'] > min_intensity]
print(f"Number of protons: {len(protons)}")
print(f"Number of gammas: {len(gammas)}")
Number of protons: 1425 Number of gammas: 1674
Random Events¶
In [9]:
Copied!
def plot_images_board(events):
fig, axes = plt.subplots(6, 10, figsize=(16, 9))
for i in range(60):
ax = axes.flatten()[i]
image = events[i]['image']
tel_id = events[i]['tel_id']
camera = subarray.tel[tel_id].camera
geometry = camera.geometry
display = CameraDisplay(geometry, ax=ax)
display.image = image
frame_strip(display)
plt.subplots_adjust(wspace=0, hspace=0) # Remove space between subplots
plt.tight_layout()
def plot_images_board(events):
fig, axes = plt.subplots(6, 10, figsize=(16, 9))
for i in range(60):
ax = axes.flatten()[i]
image = events[i]['image']
tel_id = events[i]['tel_id']
camera = subarray.tel[tel_id].camera
geometry = camera.geometry
display = CameraDisplay(geometry, ax=ax)
display.image = image
frame_strip(display)
plt.subplots_adjust(wspace=0, hspace=0) # Remove space between subplots
plt.tight_layout()
In [10]:
Copied!
plot_images_board(gammas)
plt.title('Gamma events')
plt.show()
plot_images_board(gammas)
plt.title('Gamma events')
plt.show()
In [11]:
Copied!
plot_images_board(protons)
plt.title('Proton events')
plt.show()
plot_images_board(protons)
plt.title('Proton events')
plt.show()
In [ ]:
Copied!